/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ejb;

import com.model.Usuario;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

/**
 *
 * @author juan_antonio
 */
@Stateless
public class UsuarioFacade extends AbstractFacade<Usuario> implements UsuarioFacadeLocal {
    @PersistenceContext(unitName = "MailServer-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UsuarioFacade() {
        super(Usuario.class);
    }
    
    @Override
    public Usuario findByEmailAndPassword(String email, String password){
        try {
            return (Usuario) em.createQuery("SELECT u FROM Usuario u WHERE u.email LIKE :usmail AND u.password LIKE :uspassword").
                    setParameter("usmail", email).
                    setParameter("uspassword", password).
                    getSingleResult();
        } catch (NoResultException ex) {
            return null;
        }
    }
    
    @Override
    public Usuario findByEmail(String email){
        try {
            return (Usuario) em.createQuery("SELECT u FROM Usuario u WHERE u.email LIKE :usmail").
                    setParameter("usmail", email).
                    getSingleResult();
        } catch (NoResultException ex) {
            return null;
        }
    }
    
}
